/*
 * @Author: MrDotYan 2564217790@qq.com
 * @Date: 2024-09-07 13:55:58
 * @LastEditors: MrDotYan 2564217790@qq.com
 * @LastEditTime: 2024-09-21 13:42:09
 * @FilePath: /webServer/src/controller/menus/menus.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import {
  Body,
  Controller,
  Del,
  Get,
  Inject,
  Post,
  Put,
  Query,
  UseGuard,
} from '@midwayjs/core';
import { MenusService } from '../../service/menus/menus.service';
import { Validate } from '@midwayjs/validate';
import { ListParamsValidator, StrIdValidator } from '../../validator';
import {
  EditMenusValidator,
  SaveMenusValidator,
} from '../../validator/menus/menus';
import { AuthGuard } from '../../guard/role.guard';
import { Role } from '../../decorator/role.decorator';
import { MenusSchema } from '../../schema';

@UseGuard(AuthGuard)
@Controller('/menus', { tagName: '' })
export class MenusController {
  @Inject()
  menusService: MenusService;

  @Post('/list', { summary: '列表' })
  @Validate()
  async list(@Body() body: ListParamsValidator<MenusSchema>) {
    const data = await this.menusService.list(body);
    return Object.assign(data, body);
  }

  @Role(['admin'])
  @Post('/save', { summary: '保存' })
  @Validate()
  async save(@Body() body: SaveMenusValidator) {
    return await this.menusService.save(body);
  }

  @Get('/info', { summary: '信息' })
  @Validate()
  async info(@Query() body: StrIdValidator) {
    return await this.menusService.info(body.id);
  }

  @Role(['admin'])
  @Del('/remove', { summary: '删除' })
  @Validate()
  async remove(@Query() body: StrIdValidator) {
    return await this.menusService.remove(body.id);
  }

  @Role(['admin'])
  @Del('/delete', { summary: '（真）删除' })
  @Validate()
  async realRemove(@Query() body: StrIdValidator) {
    return await this.menusService.realRemove(body.id);
  }

  @Role(['admin'])
  @Put('/edit', { summary: '编辑' })
  @Validate()
  async edit(@Query() query: StrIdValidator, @Body() body: EditMenusValidator) {
    const menus = await this.menusService.info(query.id);
    Object.assign(menus, body);
    return await this.menusService.save(menus);
  }

  @Get('/trees')
  @Validate()
  async trees() {
    return await this.menusService.trees();
  }
}
